﻿#include "OiPCH.hpp"
#include "ui_OiSettingsDlg.h"

namespace Oi
{
    namespace PLScannerCore
    {
        class SettingsDlgImpl : public Impl<SettingsDlg>
        {
        public:
            void init(SettingsDlg::SettingMode mode);
            void setupParams();
            void saveParams();

            void setupLocal(const QVariantMap);
            QVariantMap local() const;

        public:
            Ui::SettingsDlg ui;
        };

        void SettingsDlgImpl::init(SettingsDlg::SettingMode mode)
        {
            OI_Q(SettingsDlg);
            ui.setupUi(q);

            setupParams();
        }

        void SettingsDlgImpl::setupParams()
        {
            OI_Q(SettingsDlg);
            ui._vel->setValue(PLScanner::instance()->acquireVel);
            ui._dir->setCurrentIndex(PLScanner::instance()->acquireDir?0:1);
            ui._dataOptionDir->setCurrentIndex(PLScanner::instance()->dataOptionDir);
            ui._captureTimeout->setCurrentIndex((0.1 * PLScanner::instance()->captureTimeout) - 1);
            ui._sideLen->setValue(PLScanner::instance()->sideLen);
            ui._dataInterval->setCurrentText(QString::number(PLScanner::instance()->pointMeasureInterval));
            ui._zeroMethod->setCurrentIndex(PLScanner::instance()->zeroMethod);
            ui._resultDir->setText(PLScanner::instance()->recordDir);
            ui._excelDir->setText(PLScanner::instance()->excelDir);
            ui._exportTimer->setValue(PLScanner::instance()->excelTimer);
            ui._switchMode->setCurrentIndex(PLScanner::instance()->paramMode);
            ui._switchDelayTime->setValue(PLScanner::instance()->switchDelayStopTime);
            ui._switchStartDelayTime->setValue(PLScanner::instance()->switchDelayStartTime);
            ui._widthHide->setCurrentIndex(PLScanner::instance()->widthHide);
            ui._offset->setValue(PLScanner::instance()->measureOffsetY_S);
            ui._widthET->setValue(PLScanner::instance()->widthETDefault);
            ui._widthWT->setValue(PLScanner::instance()->widthWTDefault);
            ui._thicknessET->setValue(PLScanner::instance()->thicknessETDefault);
            ui._thicknessWT->setValue(PLScanner::instance()->thicknessWTDefault);
            ui._midThicknessET->setValue(PLScanner::instance()->midThicknessETDefault);
            ui._midThicknessWT->setValue(PLScanner::instance()->midThicknessWTDefault);
            ui._widthRatio->setValue(PLScanner::instance()->widthRatio);
            ui._acqPointMethod->setCurrentIndex(PLScanner::instance()->measureAcqPointMethod);
            ui._acqPointLen->setValue(PLScanner::instance()->measureAcqPointLen);
            ui._offsetX->setValue(PLScanner::instance()->measureOffsetX);
            ui._opOffset->setValue(PLScanner::instance()->measureOpOffset);
            ui._osOffset->setValue(PLScanner::instance()->measureOsOffset);
            ui._midOffset->setValue(PLScanner::instance()->measureMidOffset);
            ui._dsOffset->setValue(PLScanner::instance()->measureDsOffset);
            ui._drOffset->setValue(PLScanner::instance()->measureDrOffset);
            ui._vel->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._dir->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._captureTimeout->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._sideLen->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._dataInterval->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._zeroMethod->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._resultDirButton->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._excelDirButton->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._switchMode->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._switchDelayTime->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._switchStartDelayTime->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._widthHide->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._widthET->setEnabled(UserManage::instance()->permission() & PT_SettingPermission1);
            ui._widthWT->setEnabled(UserManage::instance()->permission() & PT_SettingPermission1);
            ui._thicknessET->setEnabled(UserManage::instance()->permission() & PT_SettingPermission1);
            ui._thicknessWT->setEnabled(UserManage::instance()->permission() & PT_SettingPermission1);

            ui._midThicknessET->setEnabled(UserManage::instance()->permission() & PT_SettingPermission1);
            ui._midThicknessWT->setEnabled(UserManage::instance()->permission() & PT_SettingPermission1);
            ui._acqPointMethod->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);
            ui._acqPointLen->setEnabled(UserManage::instance()->permission() & PT_SettingBasic);

            ui._offset->setEnabled(true);
            ui._offsetX->setEnabled(true);

            ui._offsetLabel->setVisible(true);
            ui._offset->setVisible(true);

            ui._offsetXLabel->setVisible(true);
            ui._offsetX->setVisible(true);

            q->onSwitchModeChanged();

            q->connect(ui._switchMode, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), q, &SettingsDlg::onSwitchModeChanged);
            q->connect(ui._resultDirButton, &QPushButton::clicked, q, &SettingsDlg::onDirChoose);
            q->connect(ui._excelDirButton, &QPushButton::clicked, q, &SettingsDlg::onDirChoose);
        }

        void SettingsDlgImpl::saveParams()
        {
            PLScanner::instance()->acquireVel = ui._vel->value();
            PLScanner::instance()->acquireDir = ui._dir->currentIndex()==0;
            PLScanner::instance()->dataOptionDir = ui._dataOptionDir->currentIndex();
            PLScanner::instance()->captureTimeout = (ui._captureTimeout->currentIndex() + 1) * 10;
            PLScanner::instance()->sideLen = ui._sideLen->value();
            PLScanner::instance()->pointMeasureInterval = ui._dataInterval->currentText().toDouble();
            PLScanner::instance()->zeroMethod = ui._zeroMethod->currentIndex();
            PLScanner::instance()->recordDir = ui._resultDir->text();
            PLScanner::instance()->excelDir = ui._excelDir->text();
            PLScanner::instance()->excelTimer = ui._exportTimer->value();
            PLScanner::instance()->paramMode = ui._switchMode->currentIndex();
            PLScanner::instance()->switchDelayStopTime = ui._switchDelayTime->value();
            PLScanner::instance()->switchDelayStartTime = ui._switchStartDelayTime->value();
            PLScanner::instance()->widthHide = ui._widthHide->currentIndex();
            PLScanner::instance()->measureOffsetY_S = ui._offset->value();
            PLScanner::instance()->measureOffsetX = ui._offsetX->value();
            PLScanner::instance()->widthWTDefault = ui._widthWT->value();
            PLScanner::instance()->widthETDefault = ui._widthET->value();
            PLScanner::instance()->widthRatio = ui._widthRatio->value();
            PLScanner::instance()->thicknessWTDefault = ui._thicknessWT->value();
            PLScanner::instance()->thicknessETDefault = ui._thicknessET->value();
            PLScanner::instance()->midThicknessWTDefault = ui._midThicknessWT->value();
            PLScanner::instance()->midThicknessETDefault = ui._midThicknessET->value();
            PLScanner::instance()->measureAcqPointMethod = ui._acqPointMethod->currentIndex();
            PLScanner::instance()->measureAcqPointLen = ui._acqPointLen->value();
            PLScanner::instance()->measureOpOffset = ui._opOffset->value();
            PLScanner::instance()->measureOsOffset = ui._osOffset->value();
            PLScanner::instance()->measureMidOffset = ui._midOffset->value();
            PLScanner::instance()->measureDsOffset = ui._dsOffset->value();
            PLScanner::instance()->measureDrOffset = ui._drOffset->value();
            int displayOption = 0x0000;
           
            PLScanner::instance()->profileDispalyOption = displayOption;

        }

        void SettingsDlgImpl::setupLocal(QVariantMap cm)
        {
        
        }

        QVariantMap SettingsDlgImpl::local() const
        {
            return QVariantMap();
        }

        SettingsDlg::SettingsDlg(QWidget* parent , SettingsDlg::SettingMode mode)
            :Ctrl::Dialog(parent)
        {
            OI_I(SettingsDlg)->init(mode);
        }

        void SettingsDlg::saveParams()
        {
            OI_F(SettingsDlg);

            d->saveParams();
        }

        void SettingsDlg::setLocalConfig(const QVariantMap cm)
        {
            OI_IMPL()->setupLocal(cm);
        }

        QVariantMap SettingsDlg::localConfig() const
        {
            return OI_IMPL()->local();
        }

        void SettingsDlg::accept()
        {
            __super::accept();
        }

        void SettingsDlg::onDirChoose()
        {
            OI_F(SettingsDlg);

            auto obj = sender();
            QString dir;
            if (obj == d->ui._resultDirButton)
            {
                dir = d->ui._resultDir->text();
            }
            else if (obj == d->ui._excelDirButton)
            {
                dir = d->ui._excelDir->text();
            }

            QString fileName = QFileDialog::getExistingDirectory(this, tr("选择文件夹") , dir);

            if (!fileName.isEmpty())
            {
                if (obj == d->ui._resultDirButton)
                {
                     d->ui._resultDir->setText(fileName);
                }
                else if (obj == d->ui._excelDirButton)
                {
                    d->ui._excelDir->setText(fileName);
                }
            }
        }

        void SettingsDlg::onSwitchModeChanged()
        {
            OI_F(SettingsDlg);
            bool mode2 = d->ui._switchMode->currentIndex() == 2;
            d->ui._switchDelayTime->setVisible(mode2);
            d->ui._switchDelayTimeLabel->setVisible(mode2);
            d->ui._switchStartDelayTime->setVisible(mode2);
            d->ui._switchStartDelayTimeLabel->setVisible(mode2);
        }

        QVariantMap SettingsDlg::config()
        {
            QVariantMap  cm;


            return cm;
        }

        void SettingsDlg::setConfig(const QVariantMap cm)
        {

        }
    }
}